var app = new Vue({
	el: '#app',
	data() {
		return {
			form: {
				username: '',
				passwd: '',
				remember: false
			},

			rules: {
				username: [
					{ required: true, message: '请输入用户名', trigger: 'blur' },
				],
				passwd: [
					{ required: true, message: '请输入密码', trigger: 'blur' },
				]
			}
		}
	},
	computed: {
		
	},
	mounted() {
		let oUser = localStorage.getItem('csyx_login_user');
		if(oUser) {
			oUser = JSON.parse(oUser);
			this.form.username = oUser.username;
			this.form.passwd = oUser.passwd;
			this.form.remember = true;
		}
	},
	methods: {
		onSubmit() {
			localStorage.setItem('csyx_login_user', JSON.stringify({
				username: 'admin',
				passwd: 'admin'
			}))
    	this.$refs.form.validate((valid) => {
    		if(!valid) {
    			return;
    		}
    		axios({
    			method: 'post',
    			url: BASE_API+'/login/',
    			headers: {
				    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
				  },
				  data: this.form
    		}).then(res => {
    			if(res.data.result) {
    				// 记住密码
    				if(this.form.remember) {
    					localStorage.setItem('csyx_login_user', JSON.stringify(this.form));
    				} else {
    					localStorage.removeItem('csyx_login_user');
    				}
    				window.localStorage.setItem('username', this.form.username);
    				window.location.href = './index.html';
    			} else {
    				this.$notify({
    					duration: 1000,
    					type: 'error',
	            title: '提示',
	            message: res.data.msg
    				})
    			}
    		})
    	})
		}
	}
})